package com.woniu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.HttpStatus;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.LoginUserDTO;
import com.woniu.entity.dto.QueryUserDTO;
import com.woniu.entity.vo.UserCountDayVO;
import com.woniu.entity.vo.UserLoginVO;
import com.woniu.mapper.UserMapper;
import com.woniu.pojo.User;
import com.woniu.service.IUserService;
import com.woniu.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

  @Autowired
  private UserMapper userMapper;
    @Autowired
  private RedisTemplate redisTemplate;

    @Override
    public void register(LoginUserDTO loginUserDTO) {

        //对密码进行加密操作
        String hash = Md5Utils.hash(loginUserDTO.getPassword());
        // 调用mapper保存数据
        User user=new User();
        user.setMobile(loginUserDTO.getMobile());
        user.setPassword(hash);
        user.setName(loginUserDTO.getMobile());
        userMapper.insert(user);
    }

    @Override
    public UserLoginVO login(String mobile) {
        return userMapper.login(mobile);
    }

    @Override
    public PageData search(Integer currentPage, Integer pageSize, QueryUserDTO queryUserDTO) {
        // 1. 构建查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryUserDTO.getName())) {
            wrapper.like("name", queryUserDTO.getName());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getMobile())) {
            wrapper.eq("mobile", queryUserDTO.getMobile());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getAvatar())) {
            wrapper.ge("avatar", queryUserDTO.getAvatar());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getPassword())) {
            wrapper.eq("password", queryUserDTO.getPassword());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getVip())) {
            wrapper.eq("vip", queryUserDTO.getVip());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getIsLike())) {
            wrapper.le("is_like", queryUserDTO.getIsLike());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getIsComment())) {
            wrapper.le("is_comment", queryUserDTO.getIsComment());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getUserType())) {
            wrapper.eq("user_type", queryUserDTO.getUserType());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getCreateTime())) {
            wrapper.ge("create_time", queryUserDTO.getCreateTime());
        }
        if (!StringUtils.isEmpty(queryUserDTO.getUpdateTime())) {
            wrapper.le("update_time", queryUserDTO.getUpdateTime());
        }
        // 2. 创建分页对象
        IPage<User> page = new Page<>(currentPage, pageSize);
        // 3. 调用mapper查询数据
        IPage<User> selectPage = userMapper.selectPage(page, wrapper);
        // 4. 封装返回的对象 pageData
        PageData pageData = new PageData(selectPage.getTotal(), selectPage.getRecords());
        // 5. 返回数据
        return pageData;
    }



    @Override
    public boolean nameExist(String name) {
        QueryWrapper<User> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("name", name);
        User user = userMapper.selectOne(QueryWrapper);
        if (user == null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int updateType(Integer id, Integer userType) {
        return userMapper.updateType(id,userType);
    }

    @Override
    public User searchOne(Integer id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public Integer updateOne(User user) {
        Integer flag = userMapper.updateById(user);
        return flag;
    }

    @Override
    public Integer updatePassword(Integer id, String oldPassword, String newPassword) {

        //将前端输入的旧密码进行MD5加密
        String oldHash = Md5Utils.hash(oldPassword);

        //通过用户id及加密后的输入的旧密码作为条件查询用户表 若存在该用户修改密码 若不存在返回false
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("password",oldHash);
        User user = userMapper.selectOne(queryWrapper);

        Integer flag = 0;

        //用户若存在
        if (user != null){
            //将新密码进行MD5加密
            String newHash = Md5Utils.hash(newPassword);
            user.setPassword(newHash);

            //修改密码为新密码
            flag = userMapper.updateById(user);
        }
        return flag;
    }

    @Override
    public UserLoginVO getByOpenId(String giteeId) {
        return userMapper.getByOpenId(giteeId);
    }

    @Override
    public List<UserCountDayVO> countUsersByWeek() {
        return userMapper.countUsersByWeek();
    }

    @Override
    public List<UserCountDayVO> countUsersByMonth() {
        return userMapper.countUsersByMonth();
    }

    @Override
    public List<UserCountDayVO> countUsersByToday() {
        return userMapper.countUsersByToday();
    }

    @Override
    public List<UserCountDayVO> countUsersByYesterday() {
        return userMapper.countUsersByYesterday();
    }

    @Override
    public boolean selectVip(Integer userId) {
        User user = userMapper.selectById(userId);
        if (user.getVip() == 1){
            return true;
        }
        return false;
    }

//    @Override
//    public void saveToken(LoginUserDTO loginUserDTO) {
//        redisTemplate.opsForValue().set("token",loginUserDTO);
//        Object token = redisTemplate.opsForValue().get("token");
//        System.out.println("token = " + token);
//    }

    @Override
    public User userIsExist(String name, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        wrapper.eq("password", password);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user);
    }
//    @Override
//    public User getByToken(String token) {
//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.eq("token",token);
//        User user = userMapper.selectOne(wrapper);
//        return user;
//    }
}
